<?php

namespace common\models\course;

use Yii;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%course_apply}}".
 *
 * @property int $id
 * @property string $province 省份
 * @property string $city 市
 * @property string $district 区
 * @property string $address_detail 详细地址
 * @property string $region 地区
 * @property string $begin_time 计划开始时间
 * @property int $age_min 面向年龄下限
 * @property int $age_max 面向年龄上限
 * @property int $limit_min 拟报名人数下限
 * @property int $limit_max 拟报名人数上限
 * @property string $contact 联系人
 * @property string $mobile 联系人手机号
 * @property int $audit_status 审核状态
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class CourseApply extends \common\base\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    //
    const AUDIT_STATUS_ACTIVE = 10;
    const AUDIT_STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%course_apply}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['province', 'city', 'address_detail', 'contact', 'mobile'], 'required'],
            [['begin_time', 'age_min', 'age_max', 'limit_min', 'limit_max', 'audit_status', 'status', 'created_at', 'updated_at'], 'integer'],
            [['province', 'city', 'district'], 'string', 'max' => 6],
            [['region', 'address_detail'], 'string', 'max' => 255],
            [['contact'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'province' => '省份',
            'city' => '市',
            'district' => '区',
            'address_detail' => '详细地址',
            'region' => '地区码[,]逗号分隔',
            'begin_time' => '计划开始时间',
            'age_min' => '面向年龄下限',
            'age_max' => '面向年龄上限',
            'limit_min' => '拟报名人数下限',
            'limit_max' => '拟报名人数上限',
            'contact' => '联系人',
            'mobile' => '联系方式',
            'audit_status' => '审核状态',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getCategory() {
        return $this->hasOne(CourseCategory::className(), ['id' => 'category_id']);
    }

    public function getApplyScopes() {
        return $this->hasMany(CourseApplyCategory::className(), ['apply_id' => 'id']);
    }

    public function getCategorys() {
        return $this->hasMany(CourseCategory::className(), ['id' => 'course_category_id'])
        ->via('applyScopes');
    }

    public function getScopes() {
        return ArrayHelper::getColumn($this->categorys, 'name');
    }

}
